package hb.software.student.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import hb.software.service.student.StudentManager;
import hb.software.student.entity.Student;
import edu.zut.hb.software.base.web.GenericController;

@RestController
@RequestMapping("/student")
public class StudentController extends GenericController<Student, Long, StudentManager> {

	StudentManager studentManager;

	@Autowired
	public void setStudentManager(StudentManager studentManager) {
		this.studentManager = studentManager;
		this.manager = this.studentManager;
	}

	@GetMapping("/findall/{page}/{size}")
	public Page<Student> findall(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
		Pageable pageable = PageRequest.of(page - 1, size);
		return studentManager.findAll(pageable);
	}

	@PostMapping("/save")
	public Student save(@RequestBody Student student) {
		Student result = studentManager.save(student);
		return result;
	}

	@GetMapping("/findbyid/{id}")
	public Student findById(@PathVariable("id") Long id) {
		return studentManager.findById(id);
	}

	@DeleteMapping("/delete/{id}")
	public void delete(@PathVariable("id") Long id) {
		studentManager.delete(id);
	}

	@PutMapping("/updata")
	public Student updata(@RequestBody Student student) {
		Student result = studentManager.save(student);
		return result;
	}
}
